package chapter15;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-26 下午4:15
 */
public class SQLHelper {

    public static String getInsertSql(Class<?> clazz) {
        String table = getTableName(clazz);
        StringBuilder builder = new StringBuilder();
        Map<String, String> fieldMap = DoMappingPo.getAttributeMap(clazz);
        StringBuilder columns = new StringBuilder();
        StringBuilder attributes = new StringBuilder();
        columns.append(" (");
        attributes.append(" (");
        for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
            columns.append(entry.getKey()).append(",");
            attributes.append("?,");
        }
        builder.append("INSERT INTO ").append(table).append(columns.substring(0, columns.length() - 1)).append(") VALUES").append(attributes.substring(0, attributes.length() - 1)).append(");");
        return builder.toString();
    }

    public static String getSelectSql(Class<?> clazz) {
        String table = getTableName(clazz);
        StringBuilder builder = new StringBuilder();
        StringBuilder columns = new StringBuilder();
        Map<String, String> fieldMap = PoMappingDo.getAttributeMap(clazz);
        for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
            columns.append(entry.getKey()).append(",");
        }
        builder.append("SELECT ").append(columns.substring(0, columns.length() - 1)).append(" FROM ").append(table);
        return builder.toString();
    }

    public static String getConditionSelectSql(Class<?> clazz, String condition) {
        String table = getTableName(clazz);
        StringBuilder builder = new StringBuilder();
        StringBuilder columns = new StringBuilder();
        Map<String, String> fieldMap = PoMappingDo.getAttributeMap(clazz);
        for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
            columns.append(entry.getKey()).append(",");
        }
        builder.append("SELECT ").append(columns.substring(0, columns.length() - 1)).append(" FROM ").append(table);
        if (condition != null) {
            builder.append(" WHERE ").append(condition).append(";");
        }
        return builder.toString();
    }

    public static String getUpdateSql(Class<?> clazz, String condition) {
        String table = getTableName(clazz);
        StringBuilder builder = new StringBuilder();
        StringBuilder columns = new StringBuilder();
        Map<String, String> fieldMap = PoMappingDo.getAttributeMap(clazz);
        for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
            columns.append(entry.getKey()).append("=?,");
        }
        builder.append("UPDATE ").append(table).append(" SET ").append(columns.substring(0, columns.length() - 1)).append(" WHERE ").append(condition).append(";");
        return builder.toString();
    }

    public static String getDelSql(Class<?> clazz, String condition) {
        String table = getTableName(clazz);
        StringBuilder builder = new StringBuilder();
        builder.append("DELETE FROM ").append(table).append(" WHERE ").append(condition).append(";");
        return builder.toString();
    }

    private static String getTableName(Class<?> clazz) {
        return clazz.getSimpleName().toLowerCase();
    }
}
